Skip to content

[6.x] Guard ImageGenerator against a null asset - #15447

Closed
wakqasahmed wants to merge 6 commits into
statamic:6.xfrom
wakqasahmed:fix/issue-15359-glide-null-asset
Closed

wakqasahmed wants to merge 6 commits into
statamic:6.xfrom
wakqasahmed:fix/issue-15359-glide-null-asset

Conversation

@wakqasahmed

Copy link
Copy Markdown
Contributor

Closes #15359

ImageGenerator::generateByAsset() calls $asset->isVideo() immediately, so when an asset URL resolves to null (e.g. a repository that can't find the asset by that URL — statamic/eloquent-driver#609 is one real cause) it throws Error: Call to a member function isVideo() on null instead of returning gracefully. Because that's an \Error, not an \Exception, it isn't caught by the glide tag's existing catch (\Exception) handling, so one unresolvable asset 500s the whole page instead of just being skipped.

Added an early return of '' when $asset is falsy, matching the method's existing pattern of returning '' to skip (see the isVideo() branch just below it). Added a regression test for generateByAsset(null, [...]) and confirmed it throws the exact error from the issue before the fix and passes after. Ran the full tests/Imaging + tests/Tags/GlideTest.php suites (147 tests, 297 assertions) and Pint, both clean.

lwekuiper and others added 2 commits March 30, 2026 10:12
…c#15359)

When an asset URL resolves to null (e.g. a repository that can't
find the asset by that URL, such as statamic/eloquent-driver#609),
generateByAsset() dereferenced it directly, throwing an uncaught
Error rather than the Exception the glide tag already knows how to
catch and skip. Return '' early instead, matching the existing skip
convention used elsewhere in this method.
*/
public function generateByAsset($asset, array $params)
{
if (! $asset) {

This comment was marked as outdated.

This comment was marked as outdated.

wakqasahmed

This comment was marked as outdated.

…null-asset

# Conflicts:
#	src/Imaging/ImageGenerator.php
#	tests/Imaging/ImageGeneratorTest.php
@jasonvarga jasonvarga changed the title Guard ImageGenerator against a null asset (#15359) Guard ImageGenerator against a null asset Sep 11, 2026
@duncanmcclean duncanmcclean changed the title Guard ImageGenerator against a null asset [6.x] Guard ImageGenerator against a null asset Sep 11, 2026
*/
public function generateByAsset($asset, array $params)
{
if (! $asset) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new Log::error('Cannot generate an image for a missing asset.') carries no identifying context (no item, path, or asset ID). The original issue's complaint was that a bad asset silently 500'd for a day before anyone noticed; this line does add a log, but by the time generateByAsset() sees $asset === null, the identifying info from the caller ($item in Glide::generateGlideUrl/generateImage) is already gone. Every other failure path in Glide::generate() logs $e->getMessage(), which carries exception context — this one is a flat string that will look identical for every occurrence, so it's still not possible to tell which asset/URL is failing without reproducing it.\n\nWorth either passing/logging the original $item reference here, or (per the earlier thread on this line) throwing from Glide::generateImage() so the existing catch (\\Exception $e) logs a message that includes the offending item.

…lved

Log::error() inside ImageGenerator::generateByAsset()'s null guard has
no way to know which item the caller was resolving, so every
occurrence logged an identical, context-free message. Throw instead
from Glide::generateImage() (where $item is still in scope) so the
tag's existing catch (\Exception $e) { Log::error($e->getMessage()); }
in generate() logs a message that includes the offending item.

Per @jasonvarga's follow-up review on statamic#15447.
*/
public function generateByAsset($asset, array $params)
{
if (! $asset) {

This comment was marked as outdated.

@wakqasahmed

Copy link
Copy Markdown
Contributor Author

Closing this since #15360 fixed #15359 with the AssetNotFoundException approach you suggested. Thanks for the review, @jasonvarga.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Glide tag 500s the whole page when an asset URL resolves to null (isVideo() on null)

3 participants